package com.chw.admin.usergroup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chw.constants.Constants;
import com.chw.utils.CommonFieldsUtil;

@Service
public class SysUserGroupService implements ISysUserGroupService {

	@Autowired
	private SysGroupMapper sysGroupMapper;
	
	@Autowired
	private SysGroupUserMapper sysGroupUserMapper;
	
	@Override
	public PageInfo<SysGroup> page(SysGroup record, int currentPage, int pageSize) throws Exception {
		PageHelper.startPage(currentPage, pageSize);
		List<SysGroup> list = sysGroupMapper.find(record);
		return new PageInfo<SysGroup>(list);
	}

	@Override
	public SysGroup selectById(String id) throws Exception {
		return sysGroupMapper.selectById(id);
	}

	@Override
	public void saveOrUpdate(SysGroup record) throws Exception {
		if (record != null) {
			if (StringUtils.isNotBlank(record.getId())) {
				CommonFieldsUtil.setUpdateValue(record);
				sysGroupMapper.updateByIdSelective(record);
			}else{
				CommonFieldsUtil.setCreateValue(record);
				sysGroupMapper.insert(record);
			}
		}
	}

	@Override
	public void updateStatus(String id, String status) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("status", status);
		sysGroupMapper.updateSelective(map);
	}

	@Override
	public void delete(String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("enabledFlag", Constants.ENABLED_FLAG_N);
		sysGroupMapper.updateSelective(map);
	}

	@Override
	public void addSysUserToGroup(SysGroupUser record) throws Exception {
		//查询是否已存在
		List<SysGroupUser> list = sysGroupUserMapper.find(record);
		//若存在则更新状态Y
		if(list!=null && !list.isEmpty()){
			record = list.get(0);
			CommonFieldsUtil.setUpdateValue(record);
			record.setEnabledFlag(Constants.ENABLED_FLAG_Y);
			sysGroupUserMapper.updateByIdSelective(record);
		}else{
			//不存在则新增
			CommonFieldsUtil.setCreateValue(record);
			sysGroupUserMapper.insert(record);
		}
	}

	@Override
	public List<Map<String, Object>> getUserByGroup(SysGroupUser record) throws Exception {
		return sysGroupUserMapper.findMap(record);
	}

	@Override
	public void deleteUserFormGroup(SysGroupUser record) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("w_groupid", record.getGroupid());
		map.put("w_userid", record.getUserid());
		map.put("enabledFlag", Constants.ENABLED_FLAG_N);
		sysGroupUserMapper.updateSelective(map);
	}

	@Override
	public boolean hasGroupName(String name,String id) throws Exception {
		SysGroup group = new SysGroup();
		group.setGroupname(name);
		List<SysGroup> list = sysGroupMapper.find(group);
		if(list!=null && list.size()==1)
		{
			//当做修改操作检查名称是否存在时去除id本身
			if(StringUtils.isNotBlank(id))
			{
				group = list.get(0);
				if(id.equals(group.getId())){
					return false;
				}
			}
			return true;
		}
		return false;
	}
}
